package com.sdy.apigateway.provider;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sdy.apigateway.api.AgInterfaceAuthApi;
import com.sdy.apigateway.api.dto.AgInterfaceAuthDTO;
import com.sdy.apigateway.biz.model.AgInterfaceAuth;
import com.sdy.apigateway.biz.service.AgInterfaceAuthService;
import com.sdy.common.model.Response;
import com.sdy.mvc.annotation.RemoteService;
import feign.Param;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzq
 * @since 2019-07-30
 */
@Slf4j
@RemoteService
public class AgInterfaceAuthProvider implements AgInterfaceAuthApi {
    @Autowired
    private AgInterfaceAuthService agInterfaceAuthService;

    @Override
    public Response save(@RequestPart(required = false) AgInterfaceAuthDTO agInterfaceAuthDTO) {
        AgInterfaceAuth item = formatAgInterfaceAuth(agInterfaceAuthDTO);
        item.setCreateTime(new Date());
        item.setId(null);
        item.setState(1);
        agInterfaceAuthService.save(item);
        return Response.success(1);
    }

    @Override
    public Response removeIpAuth(@RequestPart(required = false) String code,
                                 @RequestPart(required = false) String ip) {
        agInterfaceAuthService.remove(
                new LambdaQueryWrapper<AgInterfaceAuth>()
                        .eq(AgInterfaceAuth::getAppCode, code)
                        .eq(AgInterfaceAuth::getIp, ip));
        return Response.success();
    }

    @Override
    public Response removeIpAuthItem(@RequestPart(required = false) String code,
                                     @RequestPart(required = false) String ip,
                                     @RequestPart(required = false) List<Integer> apiList) {
        if (apiList.isEmpty()) {
            return Response.success();
        }
        agInterfaceAuthService.remove(
                new LambdaQueryWrapper<AgInterfaceAuth>()
                        .eq(AgInterfaceAuth::getAppCode, code)
                        .eq(AgInterfaceAuth::getIp, ip)
                        .in(AgInterfaceAuth::getResId, apiList));
        return Response.success();
    }

    @Override
    public Response<List<AgInterfaceAuthDTO>> listByCodeAndIp(@RequestPart(required = false) String code,
                                                              @RequestPart(required = false) String ip) {
        List<AgInterfaceAuth> result = agInterfaceAuthService.list(
                new LambdaQueryWrapper<AgInterfaceAuth>()
                        .eq(AgInterfaceAuth::getAppCode, code)
                        .eq(AgInterfaceAuth::getIp, ip)
        );
        List<AgInterfaceAuthDTO> r = result.stream()
                .map(this::formatAgInterfaceAuthDTO)
                .collect(Collectors.toList());
        return Response.success(r);
    }

    /**
     * 格式化参数
     */
    private AgInterfaceAuth formatAgInterfaceAuth(@RequestPart(required = false) AgInterfaceAuthDTO agInterfaceAuthDTO) {
        return new AgInterfaceAuth()
                .setApi(agInterfaceAuthDTO.getApi())
                .setCreateTime(agInterfaceAuthDTO.getCreateTime())
                .setId(agInterfaceAuthDTO.getId())
                .setIp(agInterfaceAuthDTO.getIp())
                .setResId(agInterfaceAuthDTO.getResId())
                .setState(agInterfaceAuthDTO.getState())
                .setResName(agInterfaceAuthDTO.getResName())
                .setAppCode(agInterfaceAuthDTO.getSysCode());
    }

    /**
     * 格式化参数
     */
    private AgInterfaceAuthDTO formatAgInterfaceAuthDTO(@RequestPart(required = false) AgInterfaceAuth agInterfaceAuth) {
        return new AgInterfaceAuthDTO()
                .setApi(agInterfaceAuth.getApi())
                .setCreateTime(agInterfaceAuth.getCreateTime())
                .setId(agInterfaceAuth.getId())
                .setIp(agInterfaceAuth.getIp())
                .setResId(agInterfaceAuth.getResId())
                .setState(agInterfaceAuth.getState())
                .setResName(agInterfaceAuth.getResName())
                .setSysCode(agInterfaceAuth.getAppCode());
    }
}
